﻿using OSGeo.GDAL;
using RemoteSensingImageDirectServer.Models.Map.Cache;
using RemoteSensingImageDirectServer.Untils.Source;

namespace RemoteSensingImageDirectServer.Untils.LRU
{
    public class RasterMapNode : IRasterMapNode
    {
        /// <summary>
        /// 资源是否被释放
        /// </summary>
        private bool _disposed;

        /// <summary>
        /// 节点唯一id
        /// </summary>
        private readonly string Key;

        /// <summary>
        /// 地图数据源
        /// </summary>
        private IRSIMapSource MapSource;


        public RasterMapNode(string key, string filename, string maskfilename, int epsg)
        {
            this.Key = key;
            this.MapSource = new SingleImageSource(key, filename, maskfilename, epsg);
        }

        public RasterMapNode(string key, string filename, byte[] vrtbytes, byte[]? warpbytes, string maskfilename, int epsg)
        {
            this.Key = key;
            this.MapSource = new SingleImageSource(key, filename, vrtbytes, warpbytes, maskfilename, epsg);
        }

        public RasterMapNode(string key, GroupImageCache groupImage)
        {
            this.Key = key;
            this.MapSource = new GroupImageSource(key, groupImage);
        }

        public string GetKey()
        {
           return this.Key;
        }

        public IRSIMapSource GetMapSource()
        {
            return this.MapSource;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    MapSource?.Dispose();
                }

                this._disposed = true;
            }
            else
            {
                throw new Exception("资源已被释放");
            }
        }

        
    }
}
